0001    //
0002    //  BigUInt Radix Conversion.swift
0003    //  BigInt
0004    //
0005    //  Created by Károly Lőrentey on 2016-01-03.
0006    //  Copyright © 2016 Károly Lőrentey. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    
0012    extension BigUInt: CustomStringConvertible {
0013    
0014        //MARK: Radix Conversion
0015    
0016        /// Calculates the number of numerals in a given radix that fit inside a single `Digit`.
0017        ///
0018        /// - Returns: (chars, power) where `chars` is highest that satisfy `radix^chars <= 2^Digit.width`. `power` is zero
0019        ///   if radix is a power of two; otherwise `power == radix^chars`.
0020        private static func charsPerDigitForRadix
BigUInt Radix Conversion.swift:44
        let (charsPerDigit, power) = BigUInt.charsPerDigitForRadix(radix)
BigUInt Radix Conversion.swift:97
        let (charsPerDigit, power) = BigUInt.charsPerDigitForRadix(radix)
(radix: Int) -> (chars: Int, power: Digit) { 0021 var power: Digit = 1 0022 var overflow = false 0023 var count = 0 0024 while !overflow { 0025 let (p, o) = Digit.multiplyWithOverflow(power, Digit(radix)) 0026 overflow = o 0027 if !o || p == 0 { 0028 count += 1 0029 power = p 0030 } 0031 } 0032 return (count, power) 0033 } 0034 0035 /// Initialize a big integer from an ASCII representation in a given radix. Numerals above `9` are represented by 0036 /// letters from the English alphabet. 0037 /// 0038 /// - Requires: `radix > 1 && radix < 36` 0039 /// - Parameter `text`: A string consisting of characters corresponding to numerals in the given radix. (0-9, a-z, A-Z) 0040 /// - Parameter `radix`: The base of the number system to use, or 10 if unspecified. 0041 /// - Returns: The integer represented by `text`, or nil if `text` contains a character that does not represent a numeral in `radix`. 0042 public init
BigInt.swift:87
        guard let abs = BigUInt(text, radix: radix) else { return nil }
BigUInt.swift:89
        self = BigUInt(String(value), radix: 10)!
BigUInt.swift:95
        self = BigUInt(value, radix: 10)!
BigUInt.swift:101
        self = BigUInt(value, radix: 10)!
?(_ text: String, radix: Int = 10) { 0043 precondition(radix > 1) 0044 let (charsPerDigit, power) = BigUInt.charsPerDigitForRadix(radix) 0045 0046 var digits: [Digit] = [] 0047 var piece: String = "" 0048 var count = 0 0049 for c in text.characters.reverse() { 0050 piece.insert(c, atIndex: piece.startIndex) 0051 count += 1 0052 if count == charsPerDigit { 0053 guard let d = Digit(piece, radix: radix) else { return nil } 0054 digits.append(d) 0055 piece = "" 0056 count = 0 0057 } 0058 } 0059 if !piece.isEmpty { 0060 guard let d = Digit(piece, radix: radix) else { return nil } 0061 digits.append(d) 0062 } 0063 0064 if power == 0 { 0065 self.init(digits) 0066 } 0067 else { 0068 self.init() 0069 for d in digits.reverse() { 0070 self.multiplyInPlaceByDigit(power) 0071 self.addDigitInPlace(d) 0072 } 0073 } 0074 } 0075 0076 /// Return the decimal representation of this integer. 0077 public var description: String { 0078 return String(self, radix: 10) 0079 } 0080 } 0081 0082 extension String { 0083 /// Initialize a new string with the base-10 representation of an unsigned big integer. 0084 /// 0085 /// - Complexity: O(v.count^2) 0086 public init
BigUInt Radix Conversion.swift:134
        let text = String(self)
(_ v: BigUInt) { self.init(v, radix: 10, uppercase: false) } 0087 0088 /// Initialize a new string representing an unsigned big integer in the given radix (base). 0089 /// 0090 /// Numerals greater than 9 are represented as letters from the English alphabet, 0091 /// starting with `a` if `uppercase` is false or `A` otherwise. 0092 /// 0093 /// - Requires: radix > 1 && radix <= 36 0094 /// - Complexity: O(count) when radix is a power of two; otherwise O(count^2). 0095 public init
BigInt.swift:102
        self = String(value.abs, radix: radix, uppercase: uppercase)
BigUInt Radix Conversion.swift:78
        return String(self, radix: 10)
BigUInt Radix Conversion.swift:86
    public init(_ v: BigUInt) { self.init(v, radix: 10, uppercase: false) }
(_ v: BigUInt, radix: Int, uppercase: Bool = false) { 0096 precondition(radix > 1) 0097 let (charsPerDigit, power) = BigUInt.charsPerDigitForRadix(radix) 0098 0099 guard !v.isEmpty else { self = "0"; return } 0100 0101 var parts: [String] 0102 if power == 0 { 0103 parts = v.map { String($0, radix: radix, uppercase: uppercase) } 0104 } 0105 else { 0106 parts = [] 0107 var rest = v 0108 while !rest.isZero { 0109 let mod = rest.divideInPlaceByDigit(power) 0110 parts.append(String(mod, radix: radix, uppercase: uppercase)) 0111 } 0112 } 0113 assert(!parts.isEmpty) 0114 0115 self = "" 0116 var first = true 0117 for part in parts.reverse() { 0118 let zeroes = charsPerDigit - part.characters.count 0119 assert(zeroes >= 0) 0120 if !first && zeroes > 0 { 0121 // Insert leading zeroes for mid-digits 0122 self += String(count: zeroes, repeatedValue: "0" as Character) 0123 } 0124 first = false 0125 self += part 0126 } 0127 } 0128 } 0129 0130 extension BigUInt: CustomPlaygroundQuickLookable { 0131 /// Return the playground quick look representation of this integer. 0132 @warn_unused_result 0133 public func customPlaygroundQuickLook() -> PlaygroundQuickLook { 0134 let text = String(self) 0135 return PlaygroundQuickLook.Text(text + " (\(self.width) bits)") 0136 } 0137 } 0138